package com.boot.app.system.controller;

import cn.hutool.core.util.NumberUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.boot.api.ApiResult;
import com.boot.app.system.domain.SystemDict;
import com.boot.app.system.service.ISystemDictService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("system/dict")
public class SystemDictController {

    private final ISystemDictService service;

    @Autowired
    public SystemDictController(ISystemDictService service){
        this.service = service;
    }

    @GetMapping("{id}")
    public ApiResult<SystemDict> getDict(@PathVariable("id") Long id){
        SystemDict dict = service.getById(id);
        return ApiResult.success(dict);
    }

    @GetMapping("page/{page}/{size}")
    public ApiResult<Page<SystemDict>> getDictPage(@PathVariable("page")int page, @PathVariable("size")int size, SystemDict dict){
        LambdaQueryWrapper<SystemDict> wrapper = new LambdaQueryWrapper<>();
        if(NumberUtil.isValidNumber(dict.getId())) wrapper.like(SystemDict::getId, dict.getId());
        if(StrUtil.isNotBlank(dict.getName())) wrapper.like(SystemDict::getName, dict.getName());
        Page<SystemDict> dictPage = service.page(new Page<>(page, size), wrapper);
        return ApiResult.success(dictPage);
    }

    @PostMapping()
    public ApiResult<?> postOrPutDict(@RequestBody SystemDict dict){
        service.saveOrUpdate(dict);
        return ApiResult.success();
    }

    @DeleteMapping("{id}")
    public ApiResult<?> deleteDict(@PathVariable("id") Long id){
        service.removeById(id);
        return ApiResult.success();
    }

}
